# -*- coding: utf-8 -*-
# File : mongo_util.py
# Author: taoyahui
# Date : 2021/6/20
# Mongodb 操作工具
from pymongo import MongoClient
import configparser
import os
from utils.path_util import get_root_dir
import pymongo
import urllib.parse

cf = configparser.ConfigParser()
cf.read(os.path.join(get_root_dir(),"config.ini"))

secs = cf.sections()
host = cf.get("Mongo-Database", "host")
username_conf = cf.get("Mongo-Database", "username")
password_conf = cf.get("Mongo-Database", "password")
port = cf.get("Mongo-Database", "port")


# 创建客户端
def create_client():
    """
    创建mongodb连接的客户端(client)
    :return:
        client: 连接mongodb的客户端
    """
    username = urllib.parse.quote_plus(username_conf)
    passeword = urllib.parse.quote_plus(password_conf)
    client = pymongo.MongoClient(f"mongodb://{username}:{passeword}@{host}:{port}/")
    return client


def find_by_condition(db_name, collection_name, condition = None):
    """
    查看mongodb的数据
    :param db_name:  连接的mongodb collection
    :param collection_name: 连接的document
    :param condition: 查询的条件
    :return:
        要查询的数据
    """
    client = create_client()
    db = client[db_name]
    collection = db[collection_name]
    cursor = collection.find(condition)
    res_list = []
    for res in cursor:
        res_list.append(res)

    return res_list


# 插入一条数据
def insert_one_document(db_name, collection_name, doc):
    """
    插入一条数据
    :param db_name:
    :param collection_name:
    :param doc: 数据
    :return:
    """
    client = create_client()
    db = client[db_name]

    collection = db[collection_name]
    insert_res = collection.insert_one(doc)
    return insert_res.inserted_id


# 插入多条记录
def insert_many_document(db_name, collection_name, doc):
    """
    插入多条数据
    :param db_name:
    :param collection_name:
    :param doc:
    :return:
    """
    client = create_client()
    db = client[db_name]
    collection = db[collection_name]
    insert_res = collection.insert_many(doc)
    return insert_res.inserted_ids


# 按条件删除集合中的数据
def delete_document(db_name, collection_name, condition):
    """
    按条件删除集合中的数据
    :param db_name:
    :param collection_name:
    :param condition:
    :return:
    """
    client = create_client()
    db = client[db_name]
    collection = db[collection_name]
    collection.delete_many(condition)
    return 1


# 清空collection中的数据
def truncate_collection(db_name, collection_name):
    """
    清空某个collection
    :param db_name:
    :param collection_name:
    :return:
    """
    client = create_client()
    db = client[db_name]
    if collection_name not in db.list_collection_names():
        return 0
    collection = db[collection_name]
    collection.delete_many({})
    return 1


if __name__ == '__main__':
    # 插入一条数据(测试)
    doc = [{'name':'zhangsan'}, {'name':'lisi'}, {'name':'wangwu'}]
    print(insert_many_document('patch_distribute', 'strategy_history',doc))

    # 删除数据(测试)
    # condition = {'test':'test'}
    # print(delete_document('patch_distribute', 'strategy_history',condition))

    # print(truncate_collection('patch_distribute', 'strategy_history'))

    print(find_by_condition('patch_distribute', 'inventory_patch', {"cve_id": "CVE-2020-8647"}))







